﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace MySerialization
{
    public interface IDS
    {
        void Create(Person person);
        pList Read();
        void Update(int id, string fname, string lname, int age);
        void Delete(int id);
    }    
    
    public class DS_NET_Client : IDS
    {        
        public void Create(Person person)
        {
            string list = "";
            list += person.SaveToSCV();
            message ="create;" + list;
            connect(message);
        }

        public pList Read()
        {
            pList x = new pList();

            message ="read";
            connect(message);

            string[] strArray = message.Split(';', ':');
            for (int j = 0; j < strArray.Length; j++)
            {
                if (strArray[j] == "list")
                {
                    for (int k = j + 1; k < strArray.Length - j; k++)
                    {
                        if (strArray[k] == "Person")
                        {
                            string[] tmp = new string[5];
                            Array.Copy(strArray, k, tmp, 0, 5);
                            Person p = new Person();
                            p.LoadFromCSV(tmp);
                            x.Add(p);
                        }
                    }
                }
            }
            return x;
        }

        public void Update(int id, string fname, string lname, int age)
        {
            message ="update;" + id.ToString() + ";" + fname + ";" + lname + ";" + age.ToString();
            connect(message);
        }

        public void Delete(int id)
        {
            message ="delete;" + id.ToString();
            connect(message);
        }
        
        string message = null;
        //put message for server into method
        void connect(string messageToSend)
        {
            //get server address
            string host = System.Net.Dns.GetHostName();
            int port = 8811;
            //web client to work via http                        
            WebClient client = new WebClient();
            
            //encoding to bytes            
            byte[] msg = Encoding.ASCII.GetBytes(messageToSend);
            // method UploadData - Send message
            byte[] answer = client.UploadData("http://" + host + ":" + port + "/", msg);
            //decoding from bytes array to string
            message = Encoding.ASCII.GetString(answer);            
            client.Dispose();
            /*
            string t = "http://" + host + ":" + port + "/";// +messageToSend;
            client.OpenWriteAsync(new Uri(t));
            */
        }        
    }
}
